package com.leo;

import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import javafx.util.Duration;

/**
 * @ClassName ToggleButtonDemo
 * @Description Timeline 时间轴动画
 * @Author wangss
 * @date 2020.07.07 00:19
 * @Version 1.0
 */
public class TimelineDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox(50);
        ToggleGroup tg = new ToggleGroup();

        ToggleButton tb1 = new ToggleButton("播放");
        tb1.setToggleGroup(tg);

        ToggleButton tb2 = new ToggleButton("暂停");
        tb2.setToggleGroup(tg);

        ToggleButton tb3 = new ToggleButton("停止");
        tb3.setToggleGroup(tg);

        ToggleButton tb4 = new ToggleButton("跳转");
        tb4.setToggleGroup(tg);

        Button button = new Button("Button");

        Timeline timeline = new Timeline();
        // 设置动画帧数，帧数越大越流畅，默认60
//        Timeline timeline1 = new Timeline(20);

        // button x坐标位移
        KeyValue kv1 = new KeyValue(button.translateXProperty(), 0);
        KeyFrame kf1 = new KeyFrame(Duration.seconds(0), "kf1", new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("kf1");
            }
        }, kv1);

        // 差值动画设置
//        KeyValue kv2 = new KeyValue(button.translateXProperty(), 200,Interpolator.DISCRETE);
        KeyValue kv2 = new KeyValue(button.translateXProperty(), 200);
        KeyFrame kf2 = new KeyFrame(Duration.seconds(5), "kf2", new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                System.out.println("kf2");
            }
        }, kv2);

        timeline.getKeyFrames().addAll(kf1, kf2);
        // 延迟两秒后执行
//        timeline.setDelay(Duration.seconds(2));
        // 循环次数
//        timeline.setCycleCount(2);
        // 无线循环 + 反向转圈
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.setAutoReverse(true);
        // 播放速度
        timeline.setRate(2);


        hBox.getChildren().addAll(tb1, tb2, tb3, tb4);
        AnchorPane an = new AnchorPane();
        an.getChildren().addAll(hBox, button);
        AnchorPane.setTopAnchor(hBox, 100d);
        AnchorPane.setLeftAnchor(hBox, 100d);
        AnchorPane.setTopAnchor(button, 300d);
        AnchorPane.setLeftAnchor(button, 100d);
        Scene scene = new Scene(an);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ToggleButton");
        primaryStage.setWidth(1200d);
        primaryStage.setHeight(800d);
        primaryStage.setAlwaysOnTop(true);
        primaryStage.show();

        addAction(timeline, tb1, tb2, tb3, tb4);
    }

    private void addAction(Timeline timeline, ToggleButton tb1, ToggleButton tb2, ToggleButton tb3, ToggleButton tb4) {
        tb1.setOnAction(event -> {
            timeline.play();
        });

        tb2.setOnAction(event -> {
            timeline.pause();
        });

        tb3.setOnAction(event -> {
            timeline.stop();
        });

        tb4.setOnAction(event -> {
//            timeline.jumpTo(Duration.seconds(5));
            timeline.jumpTo("kf2");
        });
    }
}
